<template>
  <div class="about">
    <v-header />
    <el-container class="contaier">
      <div class="project-page">
        <div style="margin: 10px 10px 10px 0">
          <el-button size="small" type="primary" @click="creatFn()"
            >新建项目</el-button
          >
        </div>
        <div class="content">
          <div class="project-list">
            <el-card
              class="box-card project-item"
              v-for="(item, index) in projectList"
              :key="index"
            >
              <div slot="header" class="clearfix">
                <span class="item-name">{{ item.name }}</span>
              </div>
              <p class="item-desc">创建人：{{ item.createBy }}</p>
              <p class="item-desc">创建时间：{{ item.createTime }}</p>
              <div class="card-footer">
                <span class="link" @click="jumpToPolicyManage(item)">
                  进入空间
                </span>
              </div>
            </el-card>
          </div>
        </div>
      </div>
    </el-container>
    <!-- 新建项目弹窗 -->
    <el-dialog
      v-model="dialogVisible"
      title="新建项目"
      width="40%"
      @close="cancelFn"
    >
      <el-form
        ref="projectFormRef"
        :model="projectForm"
        :rules="projectRules"
        class="demo-ruleForm"
        label-width="120px"
        status-icon
        style="width: 50%"
      >
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="projectForm.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="域控名称" prop="domainControl">
          <el-input v-model="projectForm.domainControl" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目全名">
          <el-input v-model="fullName" disabled/>
        </el-form-item>
        <el-form-item label="项目描述" prop="">
          <el-input
            v-model="projectForm.description"
            placeholder="请输入项目描述"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button @click="cancelFn">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, reactive, onMounted, nextTick, computed, toRaw } from "vue";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import vHeader from "../components/Header.vue";
import { createApi, updateApi, queryProjectList } from "@/api/project";
import store from "@/store";

const router = useRouter();
let dialogVisible = ref(false);
let editProjectId = ref("");
const projectFormRef = ref(null);
const projectForm = reactive({
  projectName: '',
  domainControl: '',
  description: "",
});
const projectRules = reactive({
  projectName: [{ required: true, message: "请输入项目名称", trigger: "blur" }],
  domainControl: [{ required: true, message: "请输入域控名称", trigger: "blur" }]
});
let projectList = ref([]);
const fullName = computed(()=> {
  if(projectForm.projectName == ''){
    return 
  }
  return projectForm.projectName + '_' + projectForm.domainControl
})
// 查询列表
const getList = () => {
  queryProjectList().then((res) => {
    let response = res.data.data;
    projectList.value = response;
    // 查询回来的数据 存入list字段
  });
};
const creatFn = () => {
  dialogVisible.value = true;
};
const cancelFn = () => {
  dialogVisible.value = false;
  projectFormRef.value.resetFields();
  projectForm.description = "";
  projectForm.projectName = "";
  projectForm.domainControl = "";
};
// 创建项目(待联调，待补充编辑逻辑)
const submitForm = () => {
  if (!projectFormRef) return;
  projectFormRef.value.validate((valid) => {
    if (valid) {
      submitCreate();
    }
  });
};
const submitCreate = () => {
  let createBy = "";
  try {
    createBy = JSON.parse(localStorage.getItem("userInfo")).employeeId;
  } catch (e) {}
  let req = { ...projectForm, name:fullName.value, createBy };
  createApi(req).then((res) => {
    if (res?.data?.code === 200) {
      ElMessage({
        message: "新增成功",
        type: "success",
      });
    }
    cancelFn();
    getList();
  });
};
const jumpToPolicyManage = (projectItem) => {
  // 将选中的项目数据存入store
  store.commit("setProjectObj", projectItem);
  router.push({ path: "/triggerConfiguration/PolicyManage" });
};
onMounted(() => {
  getList();
});
</script>
<style lang="less" scoped>
.contaier {
  display: flex;
  height: 100vh;
}
.project-page {
  width: 100%;
  padding: 20px;
  overflow: auto;

  .content {
    margin: 20px 0;

    .project-list {
      margin-top: 20px;
      padding-bottom: 20px;
      display: flex;
      flex-wrap: wrap;
      .project-item {
        font-family: "Times New Roman";
        width: 16%;
        min-width: 250px;
        margin-right: 3%;
        margin-bottom: 30px;

        height: 240px;
        ::v-deep .el-card__header {
          padding: 15px 20px !important;
        }
        ::v-deep .el-card__body {
          padding: 0;
          .clearfix {
            text-align: center;
            background: #50a3f7;
            color: #fff;
            margin-bottom: 20px;
          }
          .card-footer {
            display: flex;
            justify-content: space-around;
            margin-top: 30px;

            border-top: 1px solid #f2f7ff;
            padding-top: 16px;
            font-size: 14px;
            .link,
            .link > a {
              cursor: pointer;
              color: #409eff;
            }
          }
        }
        .item-name {
          color: #fff;
          font-size: 18px;
          line-height: 2.2;
        }
        .item-desc {
          font-size: 16px;
          line-height: 2.4;
          margin-left: 6%;
          color: #606266;
          padding-bottom: 10px;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }

        &:hover {
          box-shadow: 10px 10px 16px 0 rgb(0 0 0 / 24%),
            0 17px 50px 0 rgb(0 0 0 / 19%);
          // color: #3a8ee6;
        }
      }
    }
  }
}
</style>
